package cn.fyl.system.controller;

import cn.fyl.basic.util.JsonResult;
import cn.fyl.basic.util.PageList;
import cn.fyl.system.domain.Role;
import cn.fyl.system.query.RoleQuery;
import cn.fyl.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *菜单管理接口
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService RoleService;



    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Role> findAll(){
        return RoleService.loadAll();
    }
    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            RoleService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Role findById(@PathVariable("id") Long id){
        return RoleService.loadById(id);
    }


    /**
     * 添加和修改操作
     * @param Role
     * @return
     */
    @PostMapping
    public JsonResult save(@RequestBody Role Role){
        try {
            if(Role.getId()==null){
                RoleService.save(Role);
            }else {
                RoleService.update(Role);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PutMapping
    public PageList<Role> queryPage(@RequestBody RoleQuery query){
        try {
            return RoleService.queryData(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Role>();
        }
    }



}
